// const { ajaxPrefilter } = require('jquery');
const mongoose = require('mongoose');
const rp = require("request-promise");

//获取电影集合 的 数据
// const getmovies=async ()=>await mongoose.model("movies").find()
const getmovies = async ({ curpage, eachpage }) => {
   const result = {
      rows: [],
      eachpage: ~~eachpage,
      curpage: ~~curpage,
      total: 0,
      maxpage: 0,
   };
   const moviceModel = mongoose.model("movies");
   result.total = await moviceModel.countDocuments();

   result.maxpage = Math.ceil(result.total / result.eachpage)

   result.rows = await moviceModel
      .find()
      .skip((result.curpage - 1) * result.eachpage)
      .limit(result.eachpage).sort({ _id: -1 })
      .exec();
   return result
}

const getAll = async () => {
   const getcinemas = await mongoose.model("cinemas").find()
   // const gettheaters =await mongoose.model("theaters").find()
   // const getmovies =await mongoose.model("movies").find()
   return { getcinemas }
}
const creatSchedule = async (aa) => mongoose.model("schedules").create(aa);

const gettheaters = (cinemasId) => mongoose.model("theaters").find({ "cinemasId": cinemasId });

// 通过id 查询电影
const getMociesFormschedules = async (_id) => {

   const movies = await mongoose.model("schedules").find({ movieId: _id }).populate("movieId").populate("cinemasId").populate('theaterId').exec();
   return { movies }

}
// 删除排片
const deletePaipian = async _id => mongoose.model("schedules").deleteOne({ _id: _id });

// 修改排片

const xiuGaiPaiPian = async ({ _id, theaterId, price, showTime, date }) => mongoose.model("schedules")
   .findByIdAndUpdate(_id, { theaterId, price, showTime, date },
      (err, ret) => {
         err ? console.log('失败', err) : console.log('成功')
      })



const schedulesOne = async _id => await mongoose.model("schedules").findOne({ _id: _id }).populate("movieId").populate("cinemasId").populate('theaterId').exec();


module.exports = { schedulesOne, getAll, creatSchedule, gettheaters, getmovies, getMociesFormschedules, deletePaipian, xiuGaiPaiPian }